﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using Net.Chirash.Twitter.TweetZone;
using Net.Chirash.Twitter.TwitterClient;
using Net.Chirash.Twitter.TweetZone.WindowComponents;
using Net.Chirash.Twitter.TweetZone.WindowComponents.Containers;

namespace Net.Chirash.Twitter.TweetZone.Tools
{
	/// <summary>
	/// SearchUsers.xaml の相互作用ロジック
	/// </summary>
	public partial class SearchUsersByKeyword : UserControl
	{
		public SearchUsersByKeyword()
		{
			InitializeComponent();
			searchWorker.DoWork += new DoWorkEventHandler(searchWorker_DoWork);
			searchWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(searchWorker_RunWorkerCompleted);
		}

		void searchWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
		{
			Indicator.IsConnecting = false;
		}

		#region Workers
		BackgroundWorker searchWorker = new BackgroundWorker();
		void searchWorker_DoWork(object sender, DoWorkEventArgs e)
		{
			var tc = new TwitterCommand(App.CurrentApp.Connection);
			var re = tc.SearchUser((string)e.Argument);

			Dispatcher.BeginInvoke((Action)(() =>
			{
				ResultWrapPanel.Children.Clear();
				foreach (var u in re)
					ResultWrapPanel.Children.Add(new UserBox(u));
			}), null);
		}

		#endregion

		//Search Start
		private void Button_Click(object sender, RoutedEventArgs e)
		{

			if (!searchWorker.IsBusy)
			{
				Indicator.IsConnecting = true;
				searchWorker.RunWorkerAsync(SearchWordTextBox.Text);
			}
		}

		private void SearchWordTextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
		{
			if (e.Key == Key.Enter && !searchWorker.IsBusy)
			{
				Indicator.IsConnecting = true;
				searchWorker.RunWorkerAsync(SearchWordTextBox.Text);
			}
		}

	}
}
